package com.ysd.lis.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.ysd.common.Result;
import com.ysd.common.dto.OrganizationDTO;
import com.ysd.constant.AlphabetArray;
import com.ysd.lis.dto.SysRoleDetailsDto;
import com.ysd.lis.entity.*;
import com.ysd.lis.mapper.*;
import com.ysd.lis.mapper.sys.*;
import com.ysd.lis.request.RolePageParam;
import com.ysd.lis.request.SysRoleDetailsParam;
import com.ysd.lis.request.roleAddParam;
import com.ysd.lis.service.*;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ysd.lis.vo.SysRoleDetailsVo;
import com.ysd.util.RedisUserManager;
import com.ysd.util.ToolsUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import static com.ysd.util.RedisUserManager.getUser;

/**
 * <p>
 * cold_chain.t_role 服务实现类
 * </p>
 *
 * @author bai
 * @since 2023-10-08
 */
@Service
public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRole> implements SysRoleService {
    @Autowired
    SysRoleMapper sysRoleMapper;
    @Autowired
    SysRoleMenuMapper sysRoleMenuMapper;
    @Autowired
    TRolePermissionMapper tRolePermissionMapper;
    @Autowired
    SysUserRoleMapper sysUserRoleMapper;
    @Autowired
    SysRoleMenuService sysRoleMenuService;
    @Autowired
    SysRoleDetailsMapper sysRoleDetailsMapper;
    @Autowired
    SysMenuMapper menuMapper;
    @Autowired
    SysPointsMapper sysPointsMapper;
    @Autowired
    SysRoleModuleMapper sysRoleModuleMapper;
    @Autowired
    SysRolePointsMapper sysRolePointsMapper;
    @Autowired
    SysMenuMapper sysMenuMapper;
    @Autowired
    SysModuleMapper sysModuleMapper;

    @Autowired
    SysUserAuthdifferMapper sysUserAuthdifferMapper;

    @Autowired
    SysUserAuthdetailMapper sysUserAuthdetailMapper;
    @Autowired
    SysRoleModuleService sysRoleModuleService;

    @Autowired
    SysRoleDetailsService sysRoleDetailsService;

    @Autowired
    SysRolePointsService sysRolePointsService;

    public Result findRoleList() {
        Map returnMap = new HashMap<>();
        List<Map> returnList = new ArrayList<>();
        LambdaQueryWrapper<SysRole> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(SysRole::getDelFlag, 0);
        SysUser sysUser = getUser();
        if (null != sysUser) {
            String orgId = sysUser.getOrgId();
            lambdaQueryWrapper.eq(SysRole::getOrgId, orgId);
        }
        List<SysRole> sysRoles = this.baseMapper.selectList(lambdaQueryWrapper);
        sysRoles.forEach(a -> {
            Map map = new HashMap();
            map.put("id", a.getId());
            map.put("code", a.getCode());
            map.put("roleName", a.getRoleName());
            returnList.add(map);
        });
        returnMap.put("roleList", returnList);
        return Result.succ(1, "获取数据成功", returnMap);
    }

    @Override
    public Result getRoleList() {
        List<Map> returnList = new ArrayList<>();
        LambdaQueryWrapper<SysRole> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(SysRole::getDelFlag, 0);
        SysUser sysUser = getUser();
        if (null != sysUser) {
            String orgId = sysUser.getOrgId();
            lambdaQueryWrapper.eq(SysRole::getOrgId, orgId);
        }
        List<SysRole> sysRoles = this.baseMapper.selectList(lambdaQueryWrapper);
        sysRoles.forEach(a -> {
            Map map = new HashMap();
            map.put("id", a.getId());
            map.put("code", a.getCode());
            map.put("roleName", a.getRoleName());
            returnList.add(map);
        });
        return Result.succ(1, "获取数据成功", returnList);
    }

    @Override
    public Result findRolePageList(RolePageParam pageParam) {
        Map returnMap = new HashMap<>();
        LambdaQueryWrapper<SysRole> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        Page<SysRole> page = new Page<SysRole>();
        page.setCurrent(pageParam.getPageIndex());
        page.setSize(pageParam.getPageSize());
        lambdaQueryWrapper.eq(SysRole::getDelFlag, 0);
        SysUser sysUser = getUser();
        if (StringUtils.isNotBlank(sysUser.getOrgId())) {
            lambdaQueryWrapper.eq(SysRole::getOrgId, sysUser.getOrgId());
        }
        if (StringUtils.isNotBlank(pageParam.getKeywords())) {
            lambdaQueryWrapper.and(wrapper -> wrapper.like(SysRole::getRoleName, pageParam.getKeywords()).or().like(SysRole::getCode, pageParam.getKeywords()));
        }
        Page<SysRole> rolePageList = sysRoleMapper.selectPage(page, lambdaQueryWrapper);
        return Result.succ(1, "获取信息成功", rolePageList);
    }

    public Result findRoleListPlatform() {
        List<Map> returnList = new ArrayList<>();
        LambdaQueryWrapper<SysRole> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(SysRole::getDelFlag, 0);
        List<SysRole> sysRoles = this.baseMapper.selectList(lambdaQueryWrapper);
        sysRoles.forEach(a -> {
            Map map = new HashMap();
            map.put("id", a.getId());
            map.put("code", a.getCode());
            map.put("roleName", a.getRoleName());
            returnList.add(map);
        });
        return Result.succ(1, "获取数据成功", returnList);
    }

    public Result findMenuList(SysMenu sysMenu) {
        Map map = new HashMap();

        List<Map> menuList = this.sysRoleMapper.getMenuList(sysMenu);
        List<Map> returnList = new ArrayList<>();
        menuList.forEach(a -> {
            if (StringUtils.isBlank((String) a.get("parent_id"))) {
                a.put("children", this.getChildren(a, menuList));
                returnList.add(a);
            }
        });
        map.put("menuList", returnList);
        return Result.succ(1, "成功", map);
    }

    public List<Map> getChildren(Map a, List<Map> menuList) {
        List<Map> result = new ArrayList();
        for (Map menu : menuList) {
            if (StringUtils.isBlank((String) menu.get("parent_id"))) {
                menu.put("parentName", "");
            }
            if (StringUtils.isNotBlank((String) menu.get("parent_id")) && menu.get("parent_id").equals(a.get("id"))) {
                menu.put("parentName", a.get("title"));
                result.add(menu);
            }
        }
        for (Map menu : result) {
            menu.put("children", getChildren(menu, menuList));
        }
        if (result.size() == 0) {
            return new ArrayList<>();
        }
        return result;

    }


    public Result getPermission() {

        List<Map> returnList = new ArrayList<>();
        List<Map> permissionClass = sysRoleMapper.getPermissionClass();
        Map returnMap = new HashMap();
        if (CollectionUtils.isNotEmpty(permissionClass)) {
            permissionClass.forEach(a -> {
                Map map = new HashMap();

                map.put("id", a.get("id"));
                map.put("name", a.get("name"));
                map.put("code", a.get("code"));

                if (StringUtils.isNotBlank((String) a.get("id"))) {
                    map.put("Permissions", sysRoleMapper.getPermissionName(a.get("id").toString()));
                }
                returnList.add(map);
            });
        }
        returnMap.put("pmsList", returnList);

        return Result.succ(1, "成功", returnMap);
    }


    public List<Map> convertListToMapList(List<SysMenu> menuJoinList) {
        List<Map> mapList = new ArrayList<>();

        for (SysMenu sysMenu : menuJoinList) {
            Map<String, Object> map = new HashMap<>();

            // 获取TMenu类的所有字段
            Field[] fields = SysMenu.class.getDeclaredFields();
            for (Field field : fields) {
                field.setAccessible(true);
                try {
                    Object value = field.get(sysMenu);
                    map.put(field.getName(), value);
                } catch (IllegalAccessException e) {
                    // 处理异常，如字段不可访问等
                    e.printStackTrace();
                }
            }

            mapList.add(map);
        }

        return mapList;
    }

    public Result addRole(roleAddParam role) {
        Boolean aBoolean = this.RepeatRoleCode(role);
        if (aBoolean) {
            return Result.fail(400, "角色编码重复", "");
        }
        Map returnMap = new HashMap();
        SysRole sysRole = new SysRole();
        BeanUtil.copyProperties(role, sysRole);
        SysUser sysUser = getUser();
        String orgId = sysUser.getOrgId();
        sysRole.setOrgId(orgId);
        sysRole.setIsOpen("1");
        this.sysRoleMapper.insert(sysRole);
        String id = sysRole.getId();
//        List<String> menus = role.getMenus();
//        if (CollectionUtils.isNotEmpty(menus)) {
//            menus.forEach(a -> {
//                SysRoleMenu tRoleMenu = new SysRoleMenu();
//                tRoleMenu.setRoleId(id);
//                tRoleMenu.setOrgId(orgId);
//                tRoleMenu.setMenuId(a);
//                tRoleMenu.setCreateTime(role.getCreateTime());
//                tRoleMenu.setCreator(role.getCreator());
//                tRoleMenu.setEditor(role.getEditor());
//                tRoleMenu.setEditTime(role.getEditTime());
//                tRoleMenu.setDelFlag(0);
//                sysRoleMenuMapper.insert(tRoleMenu);
//            });
//        }
//        List<String> permissions = role.getPermissions();
//        if (CollectionUtils.isNotEmpty(permissions)) {
//            permissions.forEach(a -> {
//                TRolePermission tRolePermission = new TRolePermission();
//                tRolePermission.setRoleId(id);
//                tRolePermission.setPermissionId(a);
//                tRolePermission.setCreateTime(role.getCreateTime());
//                tRolePermission.setCreator(role.getCreator());
//                tRolePermission.setEditor(role.getEditor());
//                tRolePermission.setEditTime(role.getEditTime());
//                tRolePermission.setDelFlag(0);
//                tRolePermission.setOrgId(orgId);
//                tRolePermissionMapper.insert(tRolePermission);
//            });
//        }
        returnMap.put("roleId", id);
        return Result.succ(1, "添加角色成功", returnMap);
    }

    public Result deleteRole(String id) {
        //删除角色前先查看一下是否有人员存在此角色
        LambdaQueryWrapper<SysUserRole> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        SysUser user = getUser();
        //  String orgId = user.getOrgId();
        lambdaQueryWrapper.eq(SysUserRole::getRoleId, id).eq(SysUserRole::getDelFlag, '0');//用户与权限关联未存故删去.eq(SysUserRole::getOrgId, orgId);
        Integer selectCount = sysUserRoleMapper.selectCount(lambdaQueryWrapper);
        if (selectCount > 0) {
            return Result.fail("有用户在使用此角色，删除失败");
        } else {
            SysRole roleUp = sysRoleMapper.selectById(id);
            roleUp.setDelFlag(1);
            int deleteById = sysRoleMapper.updateById(roleUp);
            if (deleteById > 0) {
                //删除角色，功能点，菜单,功能点中间表
                LambdaQueryWrapper<SysRoleDetails> delWrapper = new LambdaQueryWrapper<>();
                delWrapper.eq(SysRoleDetails::getRoleId, id);
                sysRoleDetailsMapper.delete(delWrapper);
                Map returnMap = new HashMap();
                returnMap.put("roleId", id);
                return Result.succ(1, "删除成功", returnMap);
            } else {
                return Result.fail("删除失败");
            }
        }
    }

    public Result findRoleInfo(String id) {

        Map returnMap = new HashMap();
        Map finallMap = new HashMap();

        SysRole sysRole = this.sysRoleMapper.selectById(id);

        returnMap.put("id", sysRole.getId());
        returnMap.put("code", sysRole.getCode());
        returnMap.put("roleName", sysRole.getRoleName());
        returnMap.put("remark", sysRole.getRemark());

        LambdaQueryWrapper<SysRoleMenu> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(SysRoleMenu::getRoleId, id).eq(SysRoleMenu::getDelFlag, 0);
        List<SysRoleMenu> sysRoleMenus = sysRoleMenuMapper.selectList(lambdaQueryWrapper);
        if (CollectionUtils.isNotEmpty(sysRoleMenus)) {
            List<String> collect = sysRoleMenus.stream().map(SysRoleMenu::getMenuId
            ).collect(Collectors.toList());
            returnMap.put("menus", collect);
        }
        LambdaQueryWrapper<TRolePermission> rolePermissionLambdaQueryWrapper = new LambdaQueryWrapper<>();
        rolePermissionLambdaQueryWrapper.eq(TRolePermission::getRoleId, id).eq(TRolePermission::getDelFlag, 0);
        List<TRolePermission> tRolePermissions = tRolePermissionMapper.selectList(rolePermissionLambdaQueryWrapper);

        if (CollectionUtils.isNotEmpty(tRolePermissions)) {
            List<String> collect = tRolePermissions.stream().map(TRolePermission::getPermissionId).collect(Collectors.toList());
            returnMap.put("permissions", collect);
        }
        finallMap.put("role", returnMap);
        return Result.succ(1, "成功", finallMap);
    }

    public Result editRole(roleAddParam role) {
        Boolean aBoolean = this.RepeatRoleCode(role);
        if (aBoolean) {
            return Result.fail(400, "角色编码重复", "");
        }
        Map returnMap = new HashMap();
        SysRole sysRole = new SysRole();
        BeanUtil.copyProperties(role, sysRole);
        this.sysRoleMapper.updateById(sysRole);
//        List<String> menus = role.getMenus();
//        List<String> permissions = role.getPermissions();
//
//
//        LambdaQueryWrapper<SysRoleMenu> lambdaQueryWrapper = new LambdaQueryWrapper<>();
//        lambdaQueryWrapper.eq(SysRoleMenu::getRoleId, role.getId());
//        sysRoleMenuMapper.delete(lambdaQueryWrapper);
//        List<SysRoleMenu> sysRoleMenuList = new ArrayList<>();
//        if (CollectionUtils.isNotEmpty(menus)) {
//            menus.forEach(a -> {
//                SysRoleMenu sysRoleMenu = new SysRoleMenu();
//                sysRoleMenu.setRoleId(role.getId());
//                sysRoleMenu.setMenuId(a);
//                sysRoleMenu.setDelFlag(0);
//                sysRoleMenu.setCreator(role.getEditor());
//                sysRoleMenu.setCreateTime(role.getEditTime());
//                //sysRoleMenuMapper.insert(sysRoleMenu);
//                sysRoleMenuList.add(sysRoleMenu);
//            });
//            sysRoleMenuService.saveBatch(sysRoleMenuList);
//        }
//
//        LambdaQueryWrapper<TRolePermission> lambdaQueryWrappers = new LambdaQueryWrapper<>();
//        lambdaQueryWrappers.eq(TRolePermission::getRoleId, role.getId());
//        tRolePermissionMapper.delete(lambdaQueryWrappers);
//
//        if (CollectionUtils.isNotEmpty(permissions)) {
//            permissions.forEach(a -> {
//                TRolePermission tRolePermission = new TRolePermission();
//                tRolePermission.setRoleId(role.getId());
//                tRolePermission.setPermissionId(a);
//                tRolePermission.setDelFlag(0);
//                tRolePermission.setCreator(role.getEditor());
//                tRolePermission.setCreateTime(role.getEditTime());
//                tRolePermissionMapper.insert(tRolePermission);
//            });
//        }
        returnMap.put("roleId", role.getId());
        return Result.succ(1, "修改角色成功", returnMap);
    }

    //校验用户编码是否重复
    public Boolean RepeatRoleCode(roleAddParam role) {
        LambdaQueryWrapper<SysRole> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(SysRole::getCode, role.getCode()).eq(SysRole::getDelFlag, 0).ne(SysRole::getId, role.getId());
        SysUser user = getUser();
        String orgId = user.getOrgId();
        lambdaQueryWrapper.eq(SysRole::getCode, role.getCode()).eq(SysRole::getDelFlag, 0).eq(SysRole::getOrgId, user.getOrgId()).ne(SysRole::getId, role.getId());
        Integer integer = sysRoleMapper.selectCount(lambdaQueryWrapper);
        if (integer > 0) {
            return true;
        } else {
            return false;
        }
    }

    @Override
    public Result getMenuAndPointsByMouldId(SysRoleDetailsParam sysRoleDetailsParam) {
        SysRoleDetailsVo sysRoleDetailsVo = new SysRoleDetailsVo();
        if (ToolsUtils.isNotEmpty(sysRoleDetailsParam)) {
            String moudleId = sysRoleDetailsParam.getMoudleId();
            //List<String> moudleIds = sysRoleDetailsParam.getMoudleIds();
            if (ToolsUtils.isEmpty(moudleId)) {
                return Result.succ(1, "暂无功能模块信息", sysRoleDetailsVo);
            }

            LambdaQueryWrapper<SysMenu> queryWrapper = new LambdaQueryWrapper<>();
            SysUser sysUser = getUser();
            queryWrapper
                    .eq(SysMenu::getOrgId, sysUser.getOrgId())
                    .eq(SysMenu::getDelFlag, 0)
                    .eq(SysMenu::getModuleId, moudleId)
                    .orderByAsc(SysMenu::getSn);

            List<SysMenu> menuList = sysMenuMapper.selectList(queryWrapper);
            List<SysMenu> returnList = new ArrayList<>();
            menuList.stream().forEach(SysMenu -> {
                if (StringUtils.isBlank(SysMenu.getParentId())) {
                    SysMenu.setChildren(getChildren(SysMenu, menuList));
                    returnList.add(SysMenu);
                }
            });


            List<SysPoints> sysPointsList = new ArrayList<SysPoints>();
            //根据菜单获取功能点
            LambdaQueryWrapper<SysPoints> sysPointsQueryWrapper = new LambdaQueryWrapper<SysPoints>();
            sysPointsQueryWrapper.eq(SysPoints::getOrgId, sysUser.getOrgId()).eq(SysPoints::getDelFlag, "0").eq(SysPoints::getModuleId, moudleId);
            sysPointsList = sysPointsMapper.selectList(sysPointsQueryWrapper);
            sysRoleDetailsVo.setMenuList(returnList);
            sysRoleDetailsVo.setPointsList(sysPointsList);
            if (ToolsUtils.isNotEmpty(sysRoleDetailsParam.getRoleId())) {
                MPJLambdaWrapper<SysRoleMenu> roleMenuQueryWrapper = new MPJLambdaWrapper<SysRoleMenu>();
                roleMenuQueryWrapper
                        .selectAll(SysRoleMenu.class)
                        .selectAs(SysMenu::getParentId, SysRoleMenu::getParentId)
                        .leftJoin(SysMenu.class, p -> p.eq(SysMenu::getId, SysRoleMenu::getMenuId).eq(SysMenu::getDelFlag, 0))
                        .eq(SysRoleMenu::getOrgId, sysUser.getOrgId())
                        .eq(SysRoleMenu::getDelFlag, "0")
                        .eq(SysRoleMenu::getRoleId, sysRoleDetailsParam.getRoleId())
                        .eq(SysRoleMenu::getModuleId, sysRoleDetailsParam.getMoudleId());
                List<SysRoleMenu> roleMenuList = sysRoleMenuMapper.selectList(roleMenuQueryWrapper);

                LambdaQueryWrapper<SysRolePoints> sysRolePointsLambdaQueryWrapper = new LambdaQueryWrapper<SysRolePoints>();
                sysRolePointsLambdaQueryWrapper.eq(SysRolePoints::getOrgId, sysUser.getOrgId()).eq(SysRolePoints::getDelFlag, "0").eq(SysRolePoints::getRoleId, sysRoleDetailsParam.getRoleId());
                List<SysRolePoints> rolePointsList = sysRolePointsMapper.selectList(sysRolePointsLambdaQueryWrapper);
                sysRoleDetailsVo.setRoleMenuList(roleMenuList);
                sysRoleDetailsVo.setRolePointsList(rolePointsList);
            }

        }
        return Result.succ(1, "获取信息成功", sysRoleDetailsVo);
    }

    public List<SysMenu> getChildren(SysMenu a, List<SysMenu> menuList) {
        List<SysMenu> result = new ArrayList();
        for (SysMenu menu : menuList) {
            if (StringUtils.isBlank(menu.getParentId())) {
                menu.setParentName("");
            }
            if (StringUtils.isNotBlank(menu.getParentId()) && menu.getParentId().equals(a.getId())) {
                menu.setParentName(a.getTitle());
                result.add(menu);
            }
        }
        for (SysMenu menu : result) {
            menu.setChildren(getChildren(menu, menuList));
        }
        if (result.size() == 0) {
            return new ArrayList<>();
        }
        return result;
    }

    @Override
    public Result getRoleAndModuleByRoleId(SysRoleDetailsParam sysRoleDetailsParam) {
        LambdaQueryWrapper<SysRoleModule> queryWrapper = new LambdaQueryWrapper<>();
        SysUser sysUser = getUser();
        queryWrapper.eq(SysRoleModule::getRoleId, sysRoleDetailsParam.getRoleId())
                .eq(SysRoleModule::getOrgId, sysUser.getOrgId())
                .eq(SysRoleModule::getDelFlag, 0);
        List<SysRoleModule> list = sysRoleModuleMapper.selectList(queryWrapper);
        return Result.succ(1, "获取信息成功", list);
    }

    //勾选模块保存权限
    @Override
    @Transactional
    public Result saveRoleByModuleIds(roleAddParam role) {
        if (ToolsUtils.isEmpty(role)) {
            return Result.fail(400, "入参不可为空", "");
        }
        String moduleId = role.getModuleId();
        if (ToolsUtils.isEmpty(moduleId)) {
            return Result.fail(400, "模块信息不可为空", "");
        }
        String roleId = role.getRoleId();
        if (ToolsUtils.isEmpty(roleId)) {
            return Result.fail(400, "角色信息不可为空", "");
        }
        //根据角色Id与模块Id更新SYS_ROLE_MODULE表 先清除再新增
        SysUser sysUser = RedisUserManager.getUser();
        LambdaQueryWrapper<SysRoleModule> sysRoleModuleWrapper = new LambdaQueryWrapper<>();
        sysRoleModuleWrapper.eq(SysRoleModule::getRoleId, roleId)
                .eq(SysRoleModule::getOrgId, sysUser.getOrgId())
                .eq(SysRoleModule::getDelFlag, 0)
                .eq(SysRoleModule::getModuleId, moduleId);
        sysRoleModuleMapper.delete(sysRoleModuleWrapper);
        //根据模块id清除sys_role_menu
        LambdaQueryWrapper<SysRoleMenu> roleMenuWrapper = new LambdaQueryWrapper<>();
        roleMenuWrapper.eq(SysRoleMenu::getRoleId, roleId)
                .eq(SysRoleMenu::getOrgId, sysUser.getOrgId())
                .eq(SysRoleMenu::getDelFlag, 0)
                .eq(SysRoleMenu::getModuleId, moduleId);
        sysRoleMenuMapper.delete(roleMenuWrapper);

        //根据模块id清除SYS_ROLE_POINTS
        LambdaQueryWrapper<SysRolePoints> rolePointsMenuWrapper = new LambdaQueryWrapper<>();
        rolePointsMenuWrapper.eq(SysRolePoints::getRoleId, roleId)
                .eq(SysRolePoints::getOrgId, sysUser.getOrgId())
                .eq(SysRolePoints::getDelFlag, 0)
                .eq(SysRolePoints::getModuleId, moduleId);
        sysRolePointsMapper.delete(rolePointsMenuWrapper);
        //批量插入权限信息
        SysRoleModule sysRoleModule = new SysRoleModule();
        sysRoleModule.setOrgId(sysUser.getOrgId());
        sysRoleModule.setModuleId(moduleId);
        sysRoleModule.setRoleId(roleId);
        sysRoleModuleMapper.insert(sysRoleModule);
        String roleModuleId = sysRoleModule.getId();

        //根据moduleId获取所有的菜单，并且设置其权限
        LambdaQueryWrapper<SysMenu> menuWrapper = new LambdaQueryWrapper<>();
        menuWrapper.eq(SysMenu::getOrgId, sysUser.getOrgId())
                .eq(SysMenu::getDelFlag, 0)
                .eq(SysMenu::getModuleId, moduleId);
        List<SysMenu> menuList = sysMenuMapper.selectList(menuWrapper);
        if (ToolsUtils.isNotEmpty(menuList)) {
            for (SysMenu menu : menuList) {
                SysRoleMenu roleMenu = new SysRoleMenu();
                roleMenu.setOrgId(sysUser.getOrgId());
                roleMenu.setRoleModuleId(roleModuleId);
                roleMenu.setModuleId(moduleId);
                roleMenu.setMenuId(menu.getId());
                roleMenu.setRoleId(roleId);
                sysRoleMenuMapper.insert(roleMenu);
            }
        }
        //根据moduleId获取所有的功能点，并且设置其权限
        LambdaQueryWrapper<SysPoints> pointsWrapper = new LambdaQueryWrapper<>();
        pointsWrapper.eq(SysPoints::getOrgId, sysUser.getOrgId())
                .eq(SysPoints::getDelFlag, 0)
                .eq(SysPoints::getModuleId, moduleId);
        List<SysPoints> points = sysPointsMapper.selectList(pointsWrapper);
        if (ToolsUtils.isNotEmpty(points)) {
            for (SysPoints point : points) {
                SysRolePoints rolePoints = new SysRolePoints();
                rolePoints.setOrgId(sysUser.getOrgId());
                rolePoints.setModuleId(moduleId);
                rolePoints.setPointsId(point.getId());
                rolePoints.setRoleModuleId(roleModuleId);
                rolePoints.setRoleId(roleId);
                sysRolePointsMapper.insert(rolePoints);
            }
        }
        //返回当前模块下菜单与功能点信息
        SysRoleDetailsVo sysRoleDetailsVo = new SysRoleDetailsVo();

        List<SysPoints> sysPointsList = new ArrayList<SysPoints>();
        //根据菜单获取功能点
        LambdaQueryWrapper<SysPoints> sysPointsQueryWrapper = new LambdaQueryWrapper<SysPoints>();
        sysPointsQueryWrapper.eq(SysPoints::getOrgId, sysUser.getOrgId()).eq(SysPoints::getDelFlag, "0").eq(SysPoints::getModuleId, moduleId);
        sysPointsList = sysPointsMapper.selectList(sysPointsQueryWrapper);
        sysRoleDetailsVo.setMenuList(menuList);
        sysRoleDetailsVo.setPointsList(sysPointsList);

        LambdaQueryWrapper<SysRoleMenu> roleMenuQueryWrapper = new LambdaQueryWrapper<SysRoleMenu>();
        roleMenuQueryWrapper.eq(SysRoleMenu::getOrgId, sysUser.getOrgId()).eq(SysRoleMenu::getDelFlag, "0").eq(SysRoleMenu::getRoleId, roleId);
        List<SysRoleMenu> roleMenuList = sysRoleMenuMapper.selectList(roleMenuQueryWrapper);

        LambdaQueryWrapper<SysRolePoints> sysRolePointsLambdaQueryWrapper = new LambdaQueryWrapper<SysRolePoints>();
        sysRolePointsLambdaQueryWrapper.eq(SysRolePoints::getOrgId, sysUser.getOrgId()).eq(SysRolePoints::getDelFlag, "0").eq(SysRolePoints::getRoleId, roleId);
        List<SysRolePoints> rolePointsList = sysRolePointsMapper.selectList(sysRolePointsLambdaQueryWrapper);
        sysRoleDetailsVo.setRoleMenuList(roleMenuList);
        sysRoleDetailsVo.setRolePointsList(rolePointsList);
        return Result.succ(1, "分配成功", sysRoleDetailsVo);
    }

    //取消勾选模块
    @Override
    @Transactional
    public Result deleteRoleByModuleId(roleAddParam role) {
        if (ToolsUtils.isEmpty(role)) {
            return Result.fail(400, "入参不可为空", "");
        }
        String moduleId = role.getModuleId();
        if (ToolsUtils.isEmpty(moduleId)) {
            return Result.fail(400, "模块信息不可为空", "");
        }
        String roleId = role.getRoleId();
        if (ToolsUtils.isEmpty(roleId)) {
            return Result.fail(400, "角色信息不可为空", "");
        }
        //根据角色Id与模块Id更新SYS_ROLE_MODULE表 先清除再新增
        SysUser sysUser = RedisUserManager.getUser();
        LambdaQueryWrapper<SysRoleModule> sysRoleModuleWrapper = new LambdaQueryWrapper<>();
        sysRoleModuleWrapper.eq(SysRoleModule::getRoleId, roleId)
                .eq(SysRoleModule::getOrgId, sysUser.getOrgId())
                .eq(SysRoleModule::getDelFlag, 0)
                .eq(SysRoleModule::getModuleId, moduleId);
        sysRoleModuleMapper.delete(sysRoleModuleWrapper);
        //根据模块id清除sys_role_menu
        LambdaQueryWrapper<SysRoleMenu> roleMenuWrapper = new LambdaQueryWrapper<>();
        roleMenuWrapper.eq(SysRoleMenu::getRoleId, roleId)
                .eq(SysRoleMenu::getOrgId, sysUser.getOrgId())
                .eq(SysRoleMenu::getDelFlag, 0)
                .eq(SysRoleMenu::getModuleId, moduleId);
        sysRoleMenuMapper.delete(roleMenuWrapper);

        //根据模块id清除SYS_ROLE_POINTS
        LambdaQueryWrapper<SysRolePoints> rolePointsMenuWrapper = new LambdaQueryWrapper<>();
        rolePointsMenuWrapper.eq(SysRolePoints::getRoleId, roleId)
                .eq(SysRolePoints::getOrgId, sysUser.getOrgId())
                .eq(SysRolePoints::getDelFlag, 0)
                .eq(SysRolePoints::getModuleId, moduleId);
        sysRolePointsMapper.delete(rolePointsMenuWrapper);
        //返回当前模块下菜单与功能点信息
        SysRoleDetailsVo sysRoleDetailsVo = new SysRoleDetailsVo();

        List<SysPoints> sysPointsList = new ArrayList<SysPoints>();
        //根据moduleId获取所有的菜单，并且设置其权限
        LambdaQueryWrapper<SysMenu> menuWrapper = new LambdaQueryWrapper<>();
        menuWrapper.eq(SysMenu::getOrgId, sysUser.getOrgId())
                .eq(SysMenu::getDelFlag, 0)
                .eq(SysMenu::getModuleId, moduleId);
        List<SysMenu> menuList = sysMenuMapper.selectList(menuWrapper);
        //根据模块获取功能点
        LambdaQueryWrapper<SysPoints> sysPointsQueryWrapper = new LambdaQueryWrapper<SysPoints>();
        sysPointsQueryWrapper.eq(SysPoints::getOrgId, sysUser.getOrgId()).eq(SysPoints::getDelFlag, "0").eq(SysPoints::getModuleId, moduleId);
        sysPointsList = sysPointsMapper.selectList(sysPointsQueryWrapper);
        sysRoleDetailsVo.setMenuList(menuList);
        sysRoleDetailsVo.setPointsList(sysPointsList);

        LambdaQueryWrapper<SysRoleMenu> roleMenuQueryWrapper = new LambdaQueryWrapper<SysRoleMenu>();
        roleMenuQueryWrapper.eq(SysRoleMenu::getOrgId, sysUser.getOrgId()).eq(SysRoleMenu::getDelFlag, "0").eq(SysRoleMenu::getRoleId, roleId);
        List<SysRoleMenu> roleMenuList = sysRoleMenuMapper.selectList(roleMenuQueryWrapper);

        LambdaQueryWrapper<SysRolePoints> sysRolePointsLambdaQueryWrapper = new LambdaQueryWrapper<SysRolePoints>();
        sysRolePointsLambdaQueryWrapper.eq(SysRolePoints::getOrgId, sysUser.getOrgId()).eq(SysRolePoints::getDelFlag, "0").eq(SysRolePoints::getRoleId, roleId);
        List<SysRolePoints> rolePointsList = sysRolePointsMapper.selectList(sysRolePointsLambdaQueryWrapper);
        sysRoleDetailsVo.setRoleMenuList(roleMenuList);
        sysRoleDetailsVo.setRolePointsList(rolePointsList);
        return Result.succ(1, "分配成功", sysRoleDetailsVo);

    }

    @Autowired
    SysUserMapper sysUserMapper;
    @Autowired
    SysModuleService sysModuleService;
    @Autowired
    SysMenuService sysMenuService;
    @Autowired
    SysUserAuthdetailService sysUserAuthdetailService;
    //勾选菜单操作
    @Override
    @Transactional
    public Result saveRoleMenu(List<roleAddParam> roles) {
        if (roles.size() <= 0) {
            return Result.fail(400, "入参不可为空", "");
        }
        //根据角色Id与模块Id更新SYS_ROLE_MODULE表 先清除再新增
        SysUser sysUser = RedisUserManager.getUser();
        String orgId = sysUser.getOrgId();

        String moduleId = roles.get(0).getModuleId();
        String roleId = roles.get(0).getRoleId();
        String roleModuleId;
        LambdaQueryWrapper<SysRoleModule> roleModuleLambdaQueryWrapper = new LambdaQueryWrapper<SysRoleModule>();
        roleModuleLambdaQueryWrapper.eq(SysRoleModule::getOrgId, orgId)
                .eq(SysRoleModule::getModuleId, moduleId)
                .eq(SysRoleModule::getRoleId, roleId)
                .eq(SysRoleModule::getDelFlag, "0");
        // 删除模块信息
        int delete = sysRoleModuleMapper.delete(roleModuleLambdaQueryWrapper);
        List<SysRoleModule> roleModules = sysRoleModuleMapper.selectList(roleModuleLambdaQueryWrapper);
        if (ToolsUtils.isEmpty(roleModules)) {
            SysRoleModule sysRoleModule = new SysRoleModule();
            sysRoleModule.setOrgId(orgId);
            sysRoleModule.setRoleId(roleId);
            sysRoleModule.setModuleId(moduleId);
            sysRoleModuleMapper.insert(sysRoleModule);
            roleModuleId = sysRoleModule.getId();
        } else {
            roleModuleId = roleModules.get(0).getId();
        }
        // 先删除roleMenu数据
        LambdaQueryWrapper<SysRoleMenu> deleteWrapper = new LambdaQueryWrapper();
        deleteWrapper.eq(SysRoleMenu::getOrgId, orgId)
                .eq(SysRoleMenu::getRoleId, roleId)
                .eq(SysRoleMenu::getModuleId, moduleId);
        sysRoleMenuMapper.delete(deleteWrapper);
        List<SysRoleMenu> collect = roles.stream().map(menu -> {
            SysRoleMenu roleMenu = new SysRoleMenu();
            roleMenu.setMenuId(menu.getId());
            roleMenu.setOrgId(orgId);
            roleMenu.setRoleId(roleId);
            roleMenu.setRoleModuleId(roleModuleId);
            roleMenu.setModuleId(moduleId);
            return roleMenu;
        }).collect(Collectors.toList());
        boolean b = sysRoleMenuService.saveBatch(collect);
        if(b){
            //先根据角色id和模块id删除SYS_USER_AUTHDETAIL的数据
            LambdaQueryWrapper<SysUserAuthdetail> deleteWrapper1 = new LambdaQueryWrapper();
            deleteWrapper1.eq(SysUserAuthdetail::getOrgId, orgId)
                    .eq(SysUserAuthdetail::getRoleId, roleId)
                    .eq(SysUserAuthdetail::getModuleId, moduleId);
            sysUserAuthdetailMapper.delete(deleteWrapper1);
            //查出角色
            SysRole byId = this.getById(roleId);
            SysModule sysModule = sysModuleService.getById(moduleId);

            //根据角色查出所有的人
            MPJLambdaWrapper<SysUser> queryWrapper = new MPJLambdaWrapper<>();
            queryWrapper.selectAll(SysUser.class)
                    .innerJoin(SysUserRole.class,p->p.eq(SysUserRole::getDelFlag,0)
                            .eq(SysUserRole::getUserId,SysUser::getId)
                            .eq(SysUserRole::getRoleId,roleId))
                    .eq(SysUser::getDelFlag,0);
            List<SysUser> sysUsers = sysUserMapper.selectList(queryWrapper);
            if(ToolsUtils.isNotEmpty(sysUsers)){
                //查出所有的sysmenu

                List<SysMenu> sysMenuList = sysMenuService.listByIds(collect.stream().map(SysRoleMenu::getMenuId).collect(Collectors.toList()));
                List<SysUserAuthdetail> addSysUserAuthdetailList = new ArrayList<>();
                for(SysUser user : sysUsers){
                    for(SysMenu sysMenu : sysMenuList){
                        SysUserAuthdetail sysUserAuthdetail = new SysUserAuthdetail();
                        sysUserAuthdetail.setUserId(user.getId());
                        sysUserAuthdetail.setUserCode(user.getUsername());
                        sysUserAuthdetail.setUserName(user.getRealname());
                        sysUserAuthdetail.setPassword(user.getPassword());
                        sysUserAuthdetail.setOrgId(user.getOrgId());
                        sysUserAuthdetail.setRoleId(roleId);
                        sysUserAuthdetail.setRoleCode(byId.getCode());
                        sysUserAuthdetail.setRoleName(byId.getRoleName());
                        sysUserAuthdetail.setModuleId(sysModule.getId());
                        sysUserAuthdetail.setModuleCode(sysModule.getCode());
                        sysUserAuthdetail.setModuleName(sysModule.getName());
                        sysUserAuthdetail.setFunctionId(sysMenu.getId());
                        sysUserAuthdetail.setFunctionName(sysMenu.getTitle());
                        sysUserAuthdetail.setAuthType("0");
                        if (StringUtils.isBlank(sysMenu.getParentId())) {
                            sysUserAuthdetail.setMenuType("0");
                        } else {
                            sysUserAuthdetail.setMenuType("1");
                        }

                        addSysUserAuthdetailList.add(sysUserAuthdetail);
                    }
                }
                sysUserAuthdetailService.saveBatch(addSysUserAuthdetailList);
            }
        }
        return Result.succ(1, "勾选菜单成功", null);
    }

    @Override
    @Transactional
    public Result saveRoleMenu(roleAddParam role) {
        String roleModuleId = "";
        if (ToolsUtils.isEmpty(role)) {
            return Result.fail(400, "入参不可为空", "");
        }
        String menuId = role.getMenuId();
        if (ToolsUtils.isEmpty(menuId)) {
            return Result.fail(400, "菜单信息不可为空", "");
        }
        String roleId = role.getRoleId();
        if (ToolsUtils.isEmpty(roleId)) {
            return Result.fail(400, "角色信息不可为空", "");
        }
        //根据角色Id与模块Id更新SYS_ROLE_MODULE表 先清除再新增
        SysUser sysUser = RedisUserManager.getUser();
        String orgId = sysUser.getOrgId();
        //根据菜单Id获取模块信息(一个菜单对应一个模块)
        SysMenu menu = menuMapper.selectById(menuId);
        if (ToolsUtils.isEmpty(menu)) {
            return Result.fail(400, "异常信息：菜单数据不存在,请维护菜单信息", "");
        }
        if (ToolsUtils.isEmpty(menu.getModuleId())) {
            return Result.fail(400, "异常信息：菜单表中,未获取到模块信息", "");
        }
        String moduleId = menu.getModuleId();
        //根据  roleId 与moduleId 获取对应关系
        LambdaQueryWrapper<SysRoleModule> roleModuleLambdaQueryWrapper = new LambdaQueryWrapper<SysRoleModule>();
        roleModuleLambdaQueryWrapper.eq(SysRoleModule::getOrgId, orgId)
                .eq(SysRoleModule::getModuleId, moduleId)
                .eq(SysRoleModule::getRoleId, roleId)
                .eq(SysRoleModule::getDelFlag, "0");
        List<SysRoleModule> roleModules = sysRoleModuleMapper.selectList(roleModuleLambdaQueryWrapper);
        if (ToolsUtils.isEmpty(roleModules)) {
            SysRoleModule sysRoleModule = new SysRoleModule();
            sysRoleModule.setOrgId(orgId);
            sysRoleModule.setRoleId(roleId);
            sysRoleModule.setModuleId(moduleId);
            sysRoleModuleMapper.insert(sysRoleModule);
            roleModuleId = sysRoleModule.getId();
        } else {
            roleModuleId = roleModules.get(0).getId();
        }

        //根据菜单id获取获取父级菜单信息
        //  SysMenu tMenu = sysMenuMapper.selectById(menuId);

        //    if(ToolsUtils.isNotEmpty(menu)){
//        String parentId = menu.getParentId();
//        if (ToolsUtils.isNotEmpty(parentId)) {
//            //根据父级菜单查询权限与menuId关联表中是否有关联关
//            LambdaQueryWrapper<SysRoleMenu> roleMenuWrapper = new LambdaQueryWrapper<SysRoleMenu>();
//            roleMenuWrapper.eq(SysRoleMenu::getOrgId, orgId)
//                    .eq(SysRoleMenu::getDelFlag, "0")
//                    .eq(SysRoleMenu::getRoleId, roleId)
//                    .eq(SysRoleMenu::getMenuId, parentId);
//            List<SysRoleMenu> roleMenuList = sysRoleMenuMapper.selectList(roleMenuWrapper);
//            if (ToolsUtils.isEmpty(roleMenuList)) {//需要存储一个父节点
//                SysRoleMenu roleMenu = new SysRoleMenu();
//                roleMenu.setMenuId(parentId);
//                roleMenu.setOrgId(orgId);
//                roleMenu.setRoleId(roleId);
//                roleMenu.setRoleModuleId(roleModuleId);
//                roleMenu.setModuleId(moduleId);
//                sysRoleMenuMapper.insert(roleMenu);
//            }

        //根据父节点获取上级节点
//               SysMenu menuByParentId = sysMenuMapper.selectById(parentId);
//               if(ToolsUtils.isNotEmpty(menuByParentId)){
//                   LambdaQueryWrapper<SysRoleMenu> roleMenuByParentIdWrapper = new LambdaQueryWrapper<SysRoleMenu>();
//                   roleMenuByParentIdWrapper.eq(SysRoleMenu::getOrgId,orgId)
//                           .eq(SysRoleMenu::getDelFlag,"0")
//                           .eq(SysRoleMenu::getRoleId,roleId)
//                           .eq(SysRoleMenu::getMenuId,menuByParentId.getParentId());
//                   List<SysRoleMenu> roleMenuList2 = sysRoleMenuMapper.selectList(roleMenuWrapper);
//                   if(ToolsUtils.isEmpty(roleMenuList2)){
//                       SysRoleMenu roleMenu = new SysRoleMenu();
//                       roleMenu.setMenuId(menuByParentId.getParentId());
//                       roleMenu.setOrgId(orgId);
//                       roleMenu.setRoleId(roleId);
//                       roleMenu.setRoleModuleId(roleModuleId);
//                       roleMenu.setModuleId(moduleId);
//                       sysRoleMenuMapper.insert(roleMenu);
//                   }
//               }
//        }

        SysRoleMenu roleMenu = new SysRoleMenu();
        roleMenu.setMenuId(menuId);
        roleMenu.setOrgId(orgId);
        roleMenu.setRoleId(roleId);
        roleMenu.setRoleModuleId(roleModuleId);
        roleMenu.setModuleId(moduleId);
        sysRoleMenuMapper.insert(roleMenu);

        LambdaQueryWrapper<SysMenu> menuLambdaQueryWrapper = new LambdaQueryWrapper<SysMenu>();
        menuLambdaQueryWrapper.eq(SysMenu::getOrgId, orgId)
                .eq(SysMenu::getParentId, menuId)
                .eq(SysMenu::getDelFlag, "0");
        List<SysMenu> menuListByParentId = sysMenuMapper.selectList(menuLambdaQueryWrapper);
        if (ToolsUtils.isNotEmpty(menuListByParentId)) {
            for (SysMenu sysMenu1 : menuListByParentId) {
                SysRoleMenu roleMenu2 = new SysRoleMenu();
                roleMenu2.setRoleId(roleId);
                roleMenu2.setMenuId(sysMenu1.getId());
                roleMenu2.setOrgId(orgId);
                roleMenu2.setRoleModuleId(roleModuleId);
                sysRoleMenuMapper.insert(roleMenu2);
            }

        }
        //处理权限差异数据

        //  }
        return Result.succ(1, "勾选菜单成功", null);
    }

    @Override
    @Transactional
    public Result deleteRoleMenu(roleAddParam role) {
        if (ToolsUtils.isEmpty(role)) {
            return Result.fail(400, "入参不可为空", "");
        }
        String menuId = role.getMenuId();
        if (ToolsUtils.isEmpty(menuId)) {
            return Result.fail(400, "菜单信息不可为空", "");
        }
        String roleId = role.getRoleId();
        if (ToolsUtils.isEmpty(roleId)) {
            return Result.fail(400, "角色信息不可为空", "");
        }
        //根据角色Id与模块Id更新SYS_ROLE_MODULE表 先清除再新增
        SysUser sysUser = RedisUserManager.getUser();
        String orgId = sysUser.getOrgId();
        //根据菜单Id获取模块信息(一个菜单对应一个模块)
        SysMenu menu = menuMapper.selectById(menuId);
        if (ToolsUtils.isEmpty(menu)) {
            return Result.fail(400, "异常信息：菜单数据不存在,请维护菜单信息", "");
        }
        if (ToolsUtils.isEmpty(menu.getModuleId())) {
            return Result.fail(400, "异常信息：菜单表中,未获取到模块信息", "");
        }
        String moduleId = menu.getModuleId();
        //根据menuId获取roleMenu中的对应关系个数
        LambdaQueryWrapper<SysRoleMenu> roleMenuLambdaQueryWrapper = new LambdaQueryWrapper<SysRoleMenu>();
        roleMenuLambdaQueryWrapper.eq(SysRoleMenu::getOrgId, orgId)
                .eq(SysRoleMenu::getModuleId, moduleId)
                .eq(SysRoleMenu::getRoleId, roleId)
                .eq(SysRoleMenu::getDelFlag, "0");
        List<SysRoleMenu> roleMenus = sysRoleMenuMapper.selectList(roleMenuLambdaQueryWrapper);

        LambdaQueryWrapper<SysRoleMenu> roleMenuLambdaDeleteWrapper = new LambdaQueryWrapper<SysRoleMenu>();
        roleMenuLambdaDeleteWrapper.eq(SysRoleMenu::getOrgId, orgId)
                .eq(SysRoleMenu::getMenuId, menuId)
                .eq(SysRoleMenu::getRoleId, roleId)
                .eq(SysRoleMenu::getDelFlag, "0");
        sysRoleMenuMapper.delete(roleMenuLambdaDeleteWrapper);

        //当取消的是父级菜单，清除所有的子菜单与关联表的关系
        LambdaQueryWrapper<SysMenu> menuLambdaQueryWrapper = new LambdaQueryWrapper<SysMenu>();
        menuLambdaQueryWrapper.eq(SysMenu::getOrgId, orgId)
                .eq(SysMenu::getParentId, menuId)
                .eq(SysMenu::getDelFlag, "0");
        List<SysMenu> menuListByParentId = sysMenuMapper.selectList(menuLambdaQueryWrapper);
        if (ToolsUtils.isNotEmpty(menuListByParentId)) {
            for (SysMenu sysMenu : menuListByParentId) {
                LambdaQueryWrapper<SysRoleMenu> roleMenuListLambdaDeleteWrapper = new LambdaQueryWrapper<SysRoleMenu>();
                roleMenuListLambdaDeleteWrapper.eq(SysRoleMenu::getOrgId, orgId)
                        .eq(SysRoleMenu::getMenuId, sysMenu.getId())
                        .eq(SysRoleMenu::getRoleId, roleId)
                        .eq(SysRoleMenu::getDelFlag, "0");
                sysRoleMenuMapper.delete(roleMenuListLambdaDeleteWrapper);
            }
        }

        if (roleMenus.size() <= 1) { //清除权限与模块关系表
            LambdaQueryWrapper<SysRoleModule> roleModuleLambdaDeleteWrapper = new LambdaQueryWrapper<SysRoleModule>();
            roleModuleLambdaDeleteWrapper.eq(SysRoleModule::getOrgId, orgId)
                    .eq(SysRoleModule::getModuleId, moduleId)
                    .eq(SysRoleModule::getRoleId, roleId)
                    .eq(SysRoleModule::getDelFlag, "0");
            sysRoleModuleMapper.delete(roleModuleLambdaDeleteWrapper);
        }


        return Result.succ(1, "取消勾选菜单成功", null);
    }

    @Override
    public Result saveRolePoint(roleAddParam role) {
        String roleModuleId = "";
        if (ToolsUtils.isEmpty(role)) {
            return Result.fail(400, "入参不可为空", "");
        }
        String pointId = role.getPointId();
        if (ToolsUtils.isEmpty(pointId)) {
            return Result.fail(400, "功能信息不可为空", "");
        }
        String roleId = role.getRoleId();
        if (ToolsUtils.isEmpty(roleId)) {
            return Result.fail(400, "角色信息不可为空", "");
        }
        //根据角色Id与模块Id更新SYS_ROLE_MODULE表 先清除再新增
        SysUser sysUser = RedisUserManager.getUser();
        String orgId = sysUser.getOrgId();
        //根据菜单Id获取模块信息(一个菜单对应一个模块)
        SysPoints sysPoints = sysPointsMapper.selectById(pointId);
        if (ToolsUtils.isEmpty(sysPoints)) {
            return Result.fail(400, "异常信息：功能数据不存在,请维护菜单信息", "");
        }
        if (ToolsUtils.isEmpty(sysPoints.getModuleId())) {
            return Result.fail(400, "异常信息：功能表中,未获取到模块信息", "");
        }
        String moduleId = sysPoints.getModuleId();
        //根据  roleId 与moduleId 获取对应关系
        LambdaQueryWrapper<SysRoleModule> roleModuleLambdaQueryWrapper = new LambdaQueryWrapper<SysRoleModule>();
        roleModuleLambdaQueryWrapper.eq(SysRoleModule::getOrgId, orgId)
                .eq(SysRoleModule::getModuleId, moduleId)
                .eq(SysRoleModule::getRoleId, roleId)
                .eq(SysRoleModule::getDelFlag, "0");
        List<SysRoleModule> roleModules = sysRoleModuleMapper.selectList(roleModuleLambdaQueryWrapper);
        if (ToolsUtils.isEmpty(roleModules)) {
            SysRoleModule sysRoleModule = new SysRoleModule();
            sysRoleModule.setOrgId(orgId);
            sysRoleModule.setRoleId(roleId);
            sysRoleModule.setModuleId(moduleId);
            sysRoleModuleMapper.insert(sysRoleModule);
            roleModuleId = sysRoleModule.getId();
        } else {
            roleModuleId = roleModules.get(0).getId();
        }
        SysRolePoints rolePoints = new SysRolePoints();
        rolePoints.setPointsId(pointId);
        rolePoints.setOrgId(orgId);
        rolePoints.setRoleId(roleId);
        rolePoints.setRoleModuleId(roleModuleId);
        rolePoints.setModuleId(moduleId);
        sysRolePointsMapper.insert(rolePoints);
        return Result.succ(1, "勾选功能成功", null);
    }

    @Override
    public Result deleteRolePoint(roleAddParam role) {

        if (ToolsUtils.isEmpty(role)) {
            return Result.fail(400, "入参不可为空", "");
        }
        String pointId = role.getPointId();
        if (ToolsUtils.isEmpty(pointId)) {
            return Result.fail(400, "功能信息不可为空", "");
        }
        String roleId = role.getRoleId();
        if (ToolsUtils.isEmpty(roleId)) {
            return Result.fail(400, "角色信息不可为空", "");
        }
        //根据角色Id与模块Id更新SYS_ROLE_MODULE表 先清除再新增
        SysUser sysUser = RedisUserManager.getUser();
        String orgId = sysUser.getOrgId();
        //根据菜单Id获取模块信息(一个菜单对应一个模块)
        SysPoints sysPoints = sysPointsMapper.selectById(pointId);
        if (ToolsUtils.isEmpty(sysPoints)) {
            return Result.fail(400, "异常信息：功能数据不存在,请维护菜单信息", "");
        }
        if (ToolsUtils.isEmpty(sysPoints.getModuleId())) {
            return Result.fail(400, "异常信息：功能表中,未获取到模块信息", "");
        }
        String moduleId = sysPoints.getModuleId();
        //根据menuId获取roleMenu中的对应关系个数
        LambdaQueryWrapper<SysRolePoints> rolePointLambdaQueryWrapper = new LambdaQueryWrapper<SysRolePoints>();
        rolePointLambdaQueryWrapper.eq(SysRolePoints::getOrgId, orgId)
                .eq(SysRolePoints::getModuleId, moduleId)
                .eq(SysRolePoints::getRoleId, roleId)
                .eq(SysRolePoints::getDelFlag, "0");
        List<SysRolePoints> rolePointsList = sysRolePointsMapper.selectList(rolePointLambdaQueryWrapper);

        LambdaQueryWrapper<SysRolePoints> rolePointsLambdaDeleteWrapper = new LambdaQueryWrapper<SysRolePoints>();
        rolePointsLambdaDeleteWrapper.eq(SysRolePoints::getOrgId, orgId)
                .eq(SysRolePoints::getPointsId, pointId)
                .eq(SysRolePoints::getRoleId, roleId)
                .eq(SysRolePoints::getDelFlag, "0");
        sysRolePointsMapper.delete(rolePointsLambdaDeleteWrapper);

        if (rolePointsList.size() <= 1) { //清除权限与功能关系表
            LambdaQueryWrapper<SysRoleModule> roleModuleLambdaDeleteWrapper = new LambdaQueryWrapper<SysRoleModule>();
            roleModuleLambdaDeleteWrapper.eq(SysRoleModule::getOrgId, orgId)
                    .eq(SysRoleModule::getModuleId, moduleId)
                    .eq(SysRoleModule::getRoleId, roleId)
                    .eq(SysRoleModule::getDelFlag, "0");
            sysRoleModuleMapper.delete(roleModuleLambdaDeleteWrapper);
        }
        return Result.succ(1, "取消勾选菜单成功", null);
    }

//    @Override
//    public Result getRoleDetails(SysRoleDetailsParam roleDetailsParam) {
//        List<SysRoleDetailsDto> roleDetailDtoList = new ArrayList<SysRoleDetailsDto>();
//        if (ToolsUtils.isEmpty(roleDetailsParam)) {
//            return Result.fail(400, "入参不可为空", "");
//        }
//        String roleId = roleDetailsParam.getRoleId();
//        if (ToolsUtils.isEmpty(roleId)) {
//            return Result.fail(400, "角色信息不可为空", "");
//        }
//        //根据角色Id与模块Id更新SYS_ROLE_MODULE表 先清除再新增
//        SysUser tUser = RedisUserManager.getUser();
//        String orgId = tUser.getOrgId();
//        //获取权限与模块关联信息
//        LambdaQueryWrapper<SysRoleModule> roleModuleLambdaWrapper = new LambdaQueryWrapper<SysRoleModule>();
//        roleModuleLambdaWrapper.eq(SysRoleModule::getOrgId,orgId)
//                .eq(SysRoleModule::getRoleId,roleId)
//                .eq(SysRoleModule::getDelFlag,"0");
//      // List<SysRoleModule> sysRoleModuleList =  sysRoleModuleMapper.selectList(roleModuleLambdaWrapper);
//       String   mergeId = "";
//        MPJLambdaWrapper<SysModule> moduleInfoByModuleIdWrapper = new MPJLambdaWrapper<>();
//        moduleInfoByModuleIdWrapper
//                .selectAll(SysModule.class)
//                .leftJoin(SysRoleModule.class,SysRoleModule::getModuleId,SysModule ::getId)
//                .eq(SysRoleModule::getRoleId,roleId )
//                .eq(SysRoleModule::getOrgId,orgId)
//                .eq(SysRoleModule::getDelFlag,"0");
//        List<SysModule> sysModules = sysModuleMapper.selectJoinList(SysModule.class, moduleInfoByModuleIdWrapper);
//        if(ToolsUtils.isNotEmpty(sysModules)){
//            for (SysModule sysModule : sysModules) {
//                SysRoleDetailsDto  roleDetailsDto = new SysRoleDetailsDto();
//                roleDetailsDto.setId(sysModule.getId());
//                roleDetailsDto.setName(sysModule.getName());
//                roleDetailsDto.setParentId(null);
//                roleDetailDtoList.add(roleDetailsDto);
//            }
//        }
//        MPJLambdaWrapper<SysMenu> menuInfoByMenuIdWrapper = new MPJLambdaWrapper<>();
//        menuInfoByMenuIdWrapper
//                .selectAll(SysMenu.class)
//                .leftJoin(SysRoleMenu.class,SysRoleMenu::getMenuId,SysMenu ::getId)
//                .eq(SysRoleMenu::getRoleId,roleId )
//                .eq(SysRoleMenu::getOrgId,orgId)
//                .eq(SysRoleMenu::getDelFlag,"0");
//        List<SysMenu> menuJoinList = sysMenuMapper.selectJoinList(SysMenu.class, menuInfoByMenuIdWrapper);
//        if(ToolsUtils.isNotEmpty(menuJoinList)){
//            for (SysMenu tMenu : menuJoinList) {
//                SysRoleDetailsDto  roleDetailsDto = new SysRoleDetailsDto();
//                roleDetailsDto.setId(tMenu.getId());
//                roleDetailsDto.setName(tMenu.getTitle());
//                roleDetailsDto.setParentId(tMenu.getModuleId());
//                roleDetailDtoList.add(roleDetailsDto);
//                mergeId = tMenu.getId();
//            }
//        }
//
//        MPJLambdaWrapper<SysPoints> pointInfoByPointIdWrapper = new MPJLambdaWrapper<>();
//        pointInfoByPointIdWrapper
//                .selectAll(SysPoints.class)
//                .leftJoin(SysRolePoints.class,SysRolePoints::getPointsId,SysPoints ::getId)
//                .eq(SysRolePoints::getRoleId,roleId )
//                .eq(SysRolePoints::getOrgId,orgId)
//                .eq(SysRolePoints::getDelFlag,"0");
//        List<SysPoints> pointsJoinList = sysPointsMapper.selectJoinList(SysPoints.class, pointInfoByPointIdWrapper);
//        if(ToolsUtils.isNotEmpty(pointsJoinList)){
//            for (SysPoints sysPoints : pointsJoinList) {
//                SysRoleDetailsDto  roleDetailsDto = new SysRoleDetailsDto();
//                roleDetailsDto.setId(sysPoints.getId());
//                roleDetailsDto.setName(sysPoints.getTitle());
//                //这里 结构需要改
//                roleDetailsDto.setParentId(mergeId);//设置成菜单的id
//                roleDetailDtoList.add(roleDetailsDto);
//            }
//        }
//
//
//        return Result.succ(1, "获取数据成功", roleDetailDtoList);
//    }


//    public Result getRoleDetails(SysRoleDetailsParam roleDetailsParam) {
//
//
//        Map<String,List<Map>> mapReturn = new HashMap<>();
//
//        List<SysRoleDetailsDto> roleDetailDtoList = new ArrayList<SysRoleDetailsDto>();
//        if (ToolsUtils.isEmpty(roleDetailsParam)) {
//            return Result.fail(400, "入参不可为空", "");
//        }
//        String roleId = roleDetailsParam.getRoleId();
//        if (ToolsUtils.isEmpty(roleId)) {
//            return Result.fail(400, "角色信息不可为空", "");
//        }
//        //根据角色Id与模块Id更新SYS_ROLE_MODULE表 先清除再新增
//        SysUser tUser = RedisUserManager.getUser();
//        String orgId = tUser.getOrgId();
//
//
//
//        MPJLambdaWrapper<SysMenu> menuInfoByMenuIdWrapper = new MPJLambdaWrapper<>();
//        menuInfoByMenuIdWrapper
//                .selectAll(SysMenu.class)
//                .leftJoin(SysRoleMenu.class,SysRoleMenu::getMenuId,SysMenu ::getId)
//                .eq(SysRoleMenu::getRoleId,roleId )
//                .eq(SysRoleMenu::getOrgId,orgId)
//                .eq(SysRoleMenu::getDelFlag,"0");
//        List<SysMenu> menuJoinList = sysMenuMapper.selectJoinList(SysMenu.class, menuInfoByMenuIdWrapper);
//
//
//        Map<String, Object> resultMap = new HashMap<>();
//
//        if(ToolsUtils.isNotEmpty(menuJoinList)){
//            List<Map> mapMenuList =  convertListToMapList(menuJoinList);
//            List<Map> returnList = new ArrayList<>();
//            mapMenuList.forEach(a -> {
//                if (StringUtils.isBlank((String) a.get("parent_id"))) {
//                    a.put("children", this.getChildren(a, mapMenuList));
//                    returnList.add(a);
//                }
//            });
//            for (Map map : returnList) {
//                String moduleId = (String)map.get("moduleId");
//                resultMap.put("m"+moduleId, map);
//            }
//        }
//        MPJLambdaWrapper<SysPoints> pointInfoByPointIdWrapper = new MPJLambdaWrapper<>();
//        pointInfoByPointIdWrapper
//                .selectAll(SysPoints.class)
//                .leftJoin(SysRolePoints.class,SysRolePoints::getPointsId,SysPoints ::getId)
//                .eq(SysRolePoints::getRoleId,roleId )
//                .eq(SysRolePoints::getOrgId,orgId)
//                .eq(SysRolePoints::getDelFlag,"0");
//        List<SysPoints> pointsJoinList = sysPointsMapper.selectJoinList(SysPoints.class, pointInfoByPointIdWrapper);
//        if(ToolsUtils.isNotEmpty(pointsJoinList)){
//            for (SysPoints points : pointsJoinList) {
//                String moduleId = points.getModuleId();
//                resultMap.put("p"+moduleId, points);
//            }
//        }
//
//
//        return Result.succ(1, "获取数据成功", resultMap);
//    }


    public Result getRoleDetails(SysRoleDetailsParam roleDetailsParam) {

        // List<SysRoleDetailsDto> roleDetailDtoList = new ArrayList<SysRoleDetailsDto>();
        if (ToolsUtils.isEmpty(roleDetailsParam)) {
            return Result.fail(400, "入参不可为空", "");
        }
        String roleId = roleDetailsParam.getRoleId();
        if (ToolsUtils.isEmpty(roleId)) {
            return Result.fail(400, "角色信息不可为空", "");
        }
        //根据角色Id与模块Id更新SYS_ROLE_MODULE表 先清除再新增
        SysUser sysUser = RedisUserManager.getUser();
        String orgId = sysUser.getOrgId();

        MPJLambdaWrapper<SysModule> moduleInfoByModuleIdWrapper = new MPJLambdaWrapper<>();
        moduleInfoByModuleIdWrapper
                .selectAll(SysModule.class)
                .leftJoin(SysRoleModule.class, SysRoleModule::getModuleId, SysModule::getId)
                .eq(SysRoleModule::getRoleId, roleId)
                .eq(SysRoleModule::getOrgId, orgId)
                .eq(SysRoleModule::getDelFlag, "0")
                .orderByAsc(SysModule::getSn);
        List<SysModule> sysModules = sysModuleMapper.selectJoinList(SysModule.class, moduleInfoByModuleIdWrapper);


        List<SysRoleDetailsDto> listDto = new ArrayList<SysRoleDetailsDto>();
        for (SysModule sysModule : sysModules) {
            SysRoleDetailsDto sysRoleDetailsDto = new SysRoleDetailsDto();
            sysRoleDetailsDto.setModuleId(sysModule.getId());
            sysRoleDetailsDto.setName(sysModule.getName());
            MPJLambdaWrapper<SysMenu> menuInfoByMenuIdWrapper = new MPJLambdaWrapper<>();
            menuInfoByMenuIdWrapper
                    .selectAll(SysMenu.class)
                    .leftJoin(SysRoleMenu.class, SysRoleMenu::getMenuId, SysMenu::getId)
                    .eq(SysRoleMenu::getRoleId, roleId)
                    .eq(SysRoleMenu::getOrgId, orgId)
                    .eq(SysRoleMenu::getModuleId, sysModule.getId())
                    .eq(SysRoleMenu::getDelFlag, "0");
            List<SysMenu> menuJoinList = sysMenuMapper.selectJoinList(SysMenu.class, menuInfoByMenuIdWrapper);
            if (ToolsUtils.isNotEmpty(menuJoinList)) {

                // List<Map> mapMenuList =  convertListToMapList(menuJoinList);
                List<Map> mapList = new ArrayList<>();

                for (SysMenu menu : menuJoinList) {
                    Map<String, Object> menuMap = new HashMap<>();
                    menuMap.put("id", menu.getId());
                    menuMap.put("title", menu.getTitle());
                    menuMap.put("parent_id", menu.getParentId());

                    mapList.add(menuMap);
                }
                List<Map> returnList = new ArrayList<>();
                mapList.forEach(a -> {
                    if (StringUtils.isBlank((String) a.get("parent_id"))) {
                        a.put("children", this.getChildren(a, mapList));
                        returnList.add(a);
                    }
                });
                sysRoleDetailsDto.setMenuList(returnList);
            }
            MPJLambdaWrapper<SysPoints> pointInfoByPointIdWrapper = new MPJLambdaWrapper<>();
            pointInfoByPointIdWrapper
                    .selectAll(SysPoints.class)
                    .leftJoin(SysRolePoints.class, SysRolePoints::getPointsId, SysPoints::getId)
                    .eq(SysRolePoints::getRoleId, roleId)
                    .eq(SysRolePoints::getOrgId, orgId)
                    .eq(SysRolePoints::getModuleId, sysModule.getId())
                    .eq(SysRolePoints::getDelFlag, "0");
            List<SysPoints> pointsJoinList = sysPointsMapper.selectJoinList(SysPoints.class, pointInfoByPointIdWrapper);
            sysRoleDetailsDto.setPointsList(pointsJoinList);
            listDto.add(sysRoleDetailsDto);
        }

        return Result.succ(1, "获取数据成功", listDto);
    }

    public Map<String, List<Map<String, Object>>> groupMapsByModuleId(List<Map<String, Object>> inputList) {
        Map<String, List<Map<String, Object>>> outputMap = new HashMap<>();

        for (Map<String, Object> map : inputList) {
            String moduleId = (String) map.get("moduleId");
            if (outputMap.containsKey(moduleId)) {
                outputMap.get(moduleId).add(map);
            } else {
                List<Map<String, Object>> newList = new ArrayList<>();
                newList.add(map);
                outputMap.put(moduleId, newList);
            }
        }

        return outputMap;
    }

    //同步数据
    public Result syncRole(OrganizationDTO organizationDTO){
        //获取索引
        String indexVal = AlphabetArray.Alphabet[organizationDTO.getOrgIndex()];
        //获取来源orgid
        String sourceOrgId = organizationDTO.getSourceOrgId();
        //获取目标orgid
        String goalOrgId = organizationDTO.getGoalOrgId();

        LambdaQueryWrapper<SysRole> sysRoleWrapper = new LambdaQueryWrapper<>();
        sysRoleWrapper.eq(SysRole::getOrgId, goalOrgId);
        sysRoleMapper.delete(sysRoleWrapper);

        LambdaQueryWrapper<SysRole> sysRoleQueryWrapper = new LambdaQueryWrapper<>();
        sysRoleQueryWrapper.eq(SysRole::getOrgId, sourceOrgId).eq(SysRole::getDelFlag,0);
        List<SysRole> sysRoleList = sysRoleMapper.selectList(sysRoleQueryWrapper);

        List<SysRole> newSysRoleList = new ArrayList<>();
        for (SysRole sysRole : sysRoleList) {
            SysRole newSysRole = new SysRole();
            BeanUtil.copyProperties(sysRole, newSysRole, "id", "orgId");
            String id = sysRole.getId();
            String newId = indexVal + id.substring(indexVal.length());
            newSysRole.setId(newId);
            newSysRole.setOrgId(goalOrgId);
            newSysRoleList.add(newSysRole);
        }
        this.saveBatch(newSysRoleList);

        sysRoleModuleMapper.delete(new LambdaQueryWrapper<SysRoleModule>().eq(SysRoleModule::getOrgId, goalOrgId).eq(SysRoleModule::getDelFlag,0));

        LambdaQueryWrapper<SysRoleModule> sysRoleModuleLambdaQueryWrapper = new LambdaQueryWrapper<>();
        sysRoleModuleLambdaQueryWrapper.eq(SysRoleModule::getOrgId, organizationDTO.getSourceOrgId()).eq(SysRoleModule::getDelFlag,0);
        List<SysRoleModule> sysRoleModuleList = sysRoleModuleMapper.selectList(sysRoleModuleLambdaQueryWrapper);
        List<SysRoleModule> newSysRoleModuleList = new ArrayList<>();
        for (SysRoleModule sysRoleModule : sysRoleModuleList) {
            SysRoleModule newSysRoleModule = new SysRoleModule();
            BeanUtil.copyProperties(sysRoleModule, newSysRoleModule, "id", "orgId","roleId","moduleId");

            String id = sysRoleModule.getId();
            String newId = indexVal + id.substring(indexVal.length());
            newSysRoleModule.setId(newId);
            newSysRoleModule.setOrgId(goalOrgId);
            String roleId = sysRoleModule.getRoleId();
            if(ToolsUtils.isNotEmpty(roleId)){
            String newRoleId = indexVal + roleId.substring(indexVal.length());
            newSysRoleModule.setRoleId(newRoleId);
            }
            String moduleId = sysRoleModule.getModuleId();
            if(ToolsUtils.isNotEmpty(moduleId)) {
                String newModuleId = indexVal + moduleId.substring(indexVal.length());
                newSysRoleModule.setModuleId(newModuleId);
            }
            newSysRoleModuleList.add(newSysRoleModule);
        }
        sysRoleModuleService.saveBatch(newSysRoleModuleList);


        //删除目标orgid的所有数据
        sysRoleMenuMapper.delete(new LambdaQueryWrapper<SysRoleMenu>().eq(SysRoleMenu::getOrgId, goalOrgId).eq(SysRoleMenu::getDelFlag,0));
        //查出来源orgid的所有数据
        LambdaQueryWrapper<SysRoleMenu> nullWrapper = new LambdaQueryWrapper<>();
        nullWrapper.eq(SysRoleMenu::getOrgId,sourceOrgId).eq(SysRoleMenu::getDelFlag,0);
        List<SysRoleMenu> sysRoleMenuList = sysRoleMenuMapper.selectList(nullWrapper);

        List<SysRoleMenu> newSysRoleMenuList = new ArrayList<>();
        for (SysRoleMenu sysRoleMenu : sysRoleMenuList) {
            SysRoleMenu newSysRoleMenu = new SysRoleMenu();
            BeanUtil.copyProperties(sysRoleMenu, newSysRoleMenu,"roleId","id","orgid","menuId","roleModuleId","moduleId");

            String id = sysRoleMenu.getId();
            String newId = indexVal + id.substring(indexVal.length());
            newSysRoleMenu.setId(newId);
            newSysRoleMenu.setOrgId(goalOrgId);

            String roleId = sysRoleMenu.getRoleId();
            if(ToolsUtils.isNotEmpty(roleId)) {
                String newRoleId = indexVal + roleId.substring(indexVal.length());
                newSysRoleMenu.setRoleId(newRoleId);
            }

            String menuId = sysRoleMenu.getMenuId();
            if(ToolsUtils.isNotEmpty(menuId)) {
                String newMenuId = indexVal + menuId.substring(indexVal.length());
                newSysRoleMenu.setMenuId(newMenuId);
            }
            String roleModuleId = sysRoleMenu.getRoleModuleId();
            if(ToolsUtils.isNotEmpty(roleModuleId)) {
                String newRoleModuleId = indexVal + roleModuleId.substring(indexVal.length());
                newSysRoleMenu.setRoleModuleId(newRoleModuleId);
            }
            String moduleId = sysRoleMenu.getModuleId();
            if(ToolsUtils.isNotEmpty(moduleId)) {
                String newModuleId = indexVal + moduleId.substring(indexVal.length());
                newSysRoleMenu.setModuleId(newModuleId);
            }
            newSysRoleMenuList.add(newSysRoleMenu);
        }

        sysRoleMenuService.saveBatch(newSysRoleMenuList);


        LambdaQueryWrapper<SysRoleDetails> sysRoleDetailsWrapper = new LambdaQueryWrapper<>();
        sysRoleDetailsWrapper.eq(SysRoleDetails::getOrgId, organizationDTO.getGoalOrgId()).eq(SysRoleDetails::getDelFlag,0);
        sysRoleDetailsMapper.delete(sysRoleDetailsWrapper);
        LambdaQueryWrapper<SysRoleDetails> sysRoleDetailsLambdaQueryWrapper = new LambdaQueryWrapper<>();
        sysRoleDetailsLambdaQueryWrapper.eq(SysRoleDetails::getOrgId, organizationDTO.getSourceOrgId()).eq(SysRoleDetails::getDelFlag,0);

        List<SysRoleDetails> sysRoleDetailsList = sysRoleDetailsMapper.selectList(sysRoleDetailsLambdaQueryWrapper);

        List<SysRoleDetails> newSysRoleDetailsList = new ArrayList<>();
        for (SysRoleDetails sysRoleDetails : sysRoleDetailsList) {

            SysRoleDetails newSysRoleDetails = new SysRoleDetails();
            BeanUtil.copyProperties(sysRoleDetails, newSysRoleDetails, "id", "orgId","roleId","moduleId","menuId","pointsId");
            String id = sysRoleDetails.getId();
            String newId = indexVal + id.substring(indexVal.length());
            newSysRoleDetails.setId(newId);
            newSysRoleDetails.setOrgId(organizationDTO.getGoalOrgId());
            String roleId = sysRoleDetails.getRoleId();
            if(ToolsUtils.isNotEmpty(roleId)) {
                String newRoleId = indexVal + roleId.substring(indexVal.length());
                newSysRoleDetails.setRoleId(newRoleId);
            }
            String moduleId = sysRoleDetails.getModuleId();
            if(ToolsUtils.isNotEmpty(moduleId)) {
                String newModuleId = indexVal + moduleId.substring(indexVal.length());
                newSysRoleDetails.setModuleId(newModuleId);
            }
            String menuId = sysRoleDetails.getMenuId();
            if(ToolsUtils.isNotEmpty(menuId)) {
                String newMenuId = indexVal + menuId.substring(indexVal.length());
                newSysRoleDetails.setMenuId(newMenuId);
            }
            String pointsId = sysRoleDetails.getPointsId();
            if(ToolsUtils.isNotEmpty(pointsId)) {
                String newPointsId = indexVal + pointsId.substring(indexVal.length());
                newSysRoleDetails.setPointsId(newPointsId);
            }
            newSysRoleDetailsList.add(newSysRoleDetails);
            // sysRoleDetailsMapper.insert(sysRoleDetails);
        }
        sysRoleDetailsService.saveBatch(newSysRoleDetailsList);


        LambdaQueryWrapper<SysRolePoints> sysRolePointsLambdaQueryWrapper = new LambdaQueryWrapper<>();
        sysRolePointsLambdaQueryWrapper.eq(SysRolePoints::getOrgId, goalOrgId).eq(SysRolePoints::getDelFlag,0);
        sysRolePointsMapper.delete(sysRolePointsLambdaQueryWrapper);

        LambdaQueryWrapper<SysRolePoints> rolePointsLambdaQueryWrapper = new LambdaQueryWrapper<>();
        rolePointsLambdaQueryWrapper.eq(SysRolePoints::getOrgId, sourceOrgId).eq(SysRolePoints::getDelFlag,0);
        List<SysRolePoints> sysRolePointsList = sysRolePointsMapper.selectList(rolePointsLambdaQueryWrapper);
        List<SysRolePoints> newSysRolePointsList = new ArrayList<>();
        for (SysRolePoints sysRolePoints : sysRolePointsList) {
            SysRolePoints newSysRolePoints = new SysRolePoints();
            BeanUtil.copyProperties(sysRolePoints, newSysRolePoints, "id", "orgId","roleId","roleModuleId","pointsId","moduleId");
            String id = sysRolePoints.getId();
            String newId = indexVal + id.substring(indexVal.length());
            newSysRolePoints.setId(newId);
            newSysRolePoints.setOrgId(goalOrgId);
            String roleId = sysRolePoints.getRoleId();
            if(ToolsUtils.isNotEmpty(roleId)) {
                String newRoleId = indexVal + roleId.substring(indexVal.length());
                newSysRolePoints.setRoleId(newRoleId);
            }
            String roleModuleId = sysRolePoints.getRoleModuleId();
            if(ToolsUtils.isNotEmpty(roleModuleId)) {
                String newRoleModuleId = indexVal + roleModuleId.substring(indexVal.length());
                newSysRolePoints.setRoleModuleId(newRoleModuleId);
            }
            String pointsId = sysRolePoints.getPointsId();
            if(ToolsUtils.isNotEmpty(pointsId)) {
                String newPointsId = indexVal + pointsId.substring(indexVal.length());
                newSysRolePoints.setPointsId(newPointsId);
            }
            String moduleId = sysRolePoints.getModuleId();
            if(ToolsUtils.isNotEmpty(moduleId)) {
                String newModuleId = indexVal + moduleId.substring(indexVal.length());
                newSysRolePoints.setModuleId(newModuleId);
            }
            newSysRolePointsList.add(newSysRolePoints);
        }
        sysRolePointsService.saveBatch(newSysRolePointsList);

        return Result.succ(1, "同步成功", null);





    }

}
